﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RsdHandler.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   Rsd Handler
//   RSD (Really Simple Discoverability) Handler
//   http://cyber.law.harvard.edu/blogs/gems/tech/rsd.html
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Api.HttpHandlers
{
    using System.Text;
    using System.Web;
    using System.Xml;

    using EcBlog.Composition;
    using EcBlog.Data;
    using EcBlog.Models;

    /// <summary>
    /// Rsd Handler
    /// RSD (Really Simple Discoverability) Handler
    /// http://cyber.law.harvard.edu/blogs/gems/tech/rsd.html
    /// </summary>
    public class RsdHandler : IHttpHandler
    {
        private readonly IEcUnitOfWork unitOfWork;

        /// <summary>
        /// Initializes a new instance of the <see cref="RsdHandler"/> class.
        /// </summary>
        public RsdHandler()
        {
            this.unitOfWork =
                CompositionDependencyResolver.StaticFactory.GetService(typeof(IEcUnitOfWork)) as IEcUnitOfWork;
        }

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            using (var rsd = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8))
            {
                rsd.Formatting = Formatting.Indented;
                rsd.WriteStartDocument();

                // Rsd tag
                rsd.WriteStartElement("rsd");
                rsd.WriteAttributeString("version", "1.0");
                rsd.WriteStartElement("service");
                rsd.WriteElementString("engineName", "1.0");
                rsd.WriteElementString("engineLink", "http://ecblog.codeplex.com/");
                rsd.WriteElementString("homePageLink", this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl]);

                // APIs
                rsd.WriteStartElement("apis");

                // MetaWeblog
                rsd.WriteStartElement("api");
                rsd.WriteAttributeString("name", "MetaWeblog");
                rsd.WriteAttributeString("preferred", "true");
                rsd.WriteAttributeString(
                    "apiLink", string.Format("{0}/metaweblog.axd", this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl]));
                 rsd.WriteAttributeString("blogID", this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl]);
                rsd.WriteEndElement();

                // End APIs
                rsd.WriteEndElement();

                // End Service
                rsd.WriteEndElement();

                // End Rsd
                rsd.WriteEndElement();

                rsd.WriteEndDocument();
            }
        }
    }
}